package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.pojo.dto.notice.NoticeAddDto;
import com.teacher.pojo.dto.notice.NoticeGetDto;
import com.teacher.pojo.dto.notice.NoticeUpdateDto;
import com.teacher.pojo.entity.Notice;
import com.teacher.pojo.vo.notice.NoticeGetVo;
import com.teacher.service.INoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-09-01
 */
@RestController
@RequestMapping("/pc/notice")
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;

    /**
     * 添加公告
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加公告 notice_add  status-》10->下架 20->上架")
    @PreAuthorize("hasAuthority('notice_add')")
    public CommonResult<String> add(@RequestBody @Validated NoticeAddDto dto) {
        return iNoticeService.add(dto);
    }

    /**
     * 更新公告
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("更新公告 notice_update")
    @PreAuthorize("hasAuthority('notice_update')")
    public CommonResult<String> update(@RequestBody @Validated NoticeUpdateDto dto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(dto, notice);
        if (iNoticeService.updateById(notice)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除公告
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除公告 notice_delete")
    @PreAuthorize("hasAuthority('notice_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iNoticeService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询公告
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询公告 notice_get")
    @PreAuthorize("hasAuthority('notice_get')")
    public CommonResult<IPage<NoticeGetVo>> getPage(NoticeGetDto dto) {
        return iNoticeService.getPage(dto);
    }

    /**
     * 普通用户查询公告
     *
     * @param dto
     * @return
     */
    @GetMapping("/customer")
    @ApiOperation("普通用户查询公告")
    public CommonResult<IPage<NoticeGetVo>> getPageByCustomer(NoticeGetDto dto) {
        return iNoticeService.getPageByCustomer(dto);
    }

}

